'''
Data classes for the splash screens
'''

from PyQt4 import QtCore
from rgeSave import *
import os
from rgeSystem import *
import rgeExtra
import rgeTileset
import rgeViews
import rgeBase

#standard data keys
TEX_NAMES_KEY = "_texture_names"
TEX_SPLIT_KEY = "_texture_split"
TEX_FILEPATHS_KEY = "_textures"
ANI_NUM_KEY = "_num_of_animations"
ANI_NAMES_KEY = "_ani_names"
ANI_FRAMES_KEY = "_ani_frames"
ANI_VALUES_KEY = "_ani_values"
ANI_OFFSETS_KEY = "_ani_offsets"
ANI_TEX_NUM_KEY = "_ani_texture_numbers"
ANI_FLIP_KEY = "_ani_flip"
ANI_SIZE_KEY = "_ani_size"
ANI_TIME_KEY = "_ani_time"
ANI_CENTER_KEY = "_ani_center"

#standard data indexes
#int
ANI_NUM_INDEX = 0
#int vecs
TEX_SPLIT_INDEX = 0
ANI_FRAMES_INDEX = 1
ANI_VALUES_INDEX = 2
ANI_OFFSETS_INDEX = 3
ANI_TEX_NUM_INDEX = 4
#float vecs
ANI_SIZE_INDEX = 0
ANI_TIME_INDEX = 1
#bool vecs
ANI_FLIP_INDEX = 0
ANI_CENTER_INDEX = 1
#strings vec
TEX_NAMES_INDEX = 0
TEX_FILEPATHS_INDEX = 1
ANI_NAMES_INDEX = 2





class SpriteData(rgeBase.BaseData):
    def __init__(self, NewName = "No_Name"):
        super(SpriteData, self).__init__(NewName)
        self.addStandard(rgeExtra.StringVecItem("None", TEX_NAMES_KEY, TEX_NAMES_INDEX))
        self.addStandard(rgeExtra.IntVecItem(1, TEX_SPLIT_KEY, TEX_SPLIT_INDEX, 2, [0, 2]))
        self.addStandard(rgeExtra.StringVecItem("", TEX_FILEPATHS_KEY, TEX_FILEPATHS_INDEX))

        self.addStandard(rgeExtra.IntItem(100, ANI_NUM_KEY, ANI_NUM_INDEX))
        self.addStandard(rgeExtra.StringVecItem("", ANI_NAMES_KEY, ANI_NAMES_INDEX))
        self.addStandard(rgeExtra.IntVecItem(1, ANI_FRAMES_KEY, ANI_FRAMES_INDEX))
        self.addStandard(rgeExtra.IntVecItem(1, ANI_VALUES_KEY, ANI_VALUES_INDEX, 3, [0, 1, 2]))
        self.addStandard(rgeExtra.IntVecItem(0, ANI_OFFSETS_KEY, ANI_OFFSETS_INDEX, 3, [0, 1, 2]))
        self.addStandard(rgeExtra.IntVecItem(0, ANI_TEX_NUM_KEY, ANI_TEX_NUM_INDEX, 2, [0, 1]))
        self.addStandard(rgeExtra.BoolVecItem(False, ANI_FLIP_KEY, ANI_FLIP_INDEX, 3, [0, 1, 2]))
        self.addStandard(rgeExtra.FloatVecItem(1, ANI_SIZE_KEY, ANI_SIZE_INDEX, 3, [0, 1, 2]))
        self.addStandard(rgeExtra.FloatVecItem(1, ANI_TIME_KEY, ANI_TIME_INDEX, 2, [0, 1]))
        self.addStandard(rgeExtra.BoolVecItem(True, ANI_CENTER_KEY, ANI_CENTER_INDEX, 2, [0, 1]))
        self.updateAnimationNum()

        self.copyAniNum = 0

        self.addTexture("","None")

        self.setFilename(SPRITE_DIR + '/' + NewName + SPRITE_EXTENSION)

    def getAnimationNum(self):
        return self.getStandard(ANI_NUM_KEY).getValue()

    def addTexture(self, NewTexLoc, Name, Split = [1,1]):
        Size = self.getStandard(TEX_NAMES_KEY).getSize([]) + 1
        self.getStandard(TEX_NAMES_KEY).setSize([], Size)
        self.getStandard(TEX_SPLIT_KEY).setSize([], Size)
        self.getStandard(TEX_FILEPATHS_KEY).setSize([], Size)
        self.setTexturePath(NewTexLoc, Size - 1)
        self.setBothSplits(Split[0], Split[1], Size - 1)
        self.setTextureName(Name, Size - 1)
        self.edited()

    def setTexturePath(self, NewValue, Index):
        self.getStandard(TEX_FILEPATHS_KEY).setValue([Index], NewValue)

    def removeTexture(self, TexNum):
        if TexNum == 0:
            return
        self.getStandard(TEX_FILEPATHS_KEY).pop([TexNum])
        self.getStandard(TEX_SPLIT_KEY).pop([TexNum])
        self.getStandard(TEX_NAMES_KEY).pop([TexNum])
        self.aniRemoveTexture(TexNum)
        self.edited()

    def getTextureName(self, Index):
        return self.getStandard(TEX_NAMES_KEY).getValue([Index])

    def getAllTexNames(self):
        return self.getStandard(TEX_NAMES_KEY).getValueList([])

    def getTexturePath(self, Index):
        return self.getStandard(TEX_FILEPATHS_KEY).getValue([Index])

    def getTextureSplit(self, Index):
        return [self.getStandard(TEX_SPLIT_KEY).getValue([Index, 0]), self.getStandard(TEX_SPLIT_KEY).getValue([Index, 1])]

    def getTextureLength(self):
        return self.getStandard(TEX_FILEPATHS_KEY).getSize([])

    def getAnimationName(self, Ani):
        return self.getStandard(ANI_NAMES_KEY).getValue([Ani])

    def getTextureValues(self, Ani, Frame):
        return [self.getStandard(ANI_VALUES_KEY).getValue([Ani, Frame, 0]), self.getStandard(ANI_VALUES_KEY).getValue([Ani, Frame, 1])]

    def getTextureAniSplit(self, Ani, Frame):
        return self.getTextureSplit(self.getTextureNum(Ani, Frame))

    def getTextureAniPath(self, Ani, Frame):
        return self.getTexturePath(self.getTextureNum(Ani, Frame))

    def getTextureNum(self, Ani, Frame):
        return self.getStandard(ANI_TEX_NUM_KEY).getValue([Ani, Frame])

    def getOffset(self, Ani, Frame):
        return [self.getStandard(ANI_OFFSETS_KEY).getValue([Ani, Frame, 0]), self.getStandard(ANI_OFFSETS_KEY).getValue([Ani, Frame, 1])]

    def getFlip(self, Ani, Frame):
        return [self.getStandard(ANI_FLIP_KEY).getValue([Ani, Frame, 0]), self.getStandard(ANI_FLIP_KEY).getValue([Ani, Frame, 1])]

    def getSize(self, Ani, Frame):
        return [self.getStandard(ANI_SIZE_KEY).getValue([Ani, Frame, 0]), self.getStandard(ANI_SIZE_KEY).getValue([Ani, Frame, 1])]

    def getTime(self, Ani, Frame):
        return self.getStandard(ANI_TIME_KEY).getValue([Ani, Frame])

    def getCenter(self, Ani, Frame):
        return self.getStandard(ANI_CENTER_KEY).getValue([Ani, Frame])

    def getFrameNum(self, Ani):
        return self.getStandard(ANI_FRAMES_KEY).getValue([Ani])

    def setBothSplits(self, X, Y, Layer):
        self.setXSplit(X, Layer)
        self.setYSplit(Y, Layer)

    def setXSplit(self, NewValue, Layer):
        self.getStandard(TEX_SPLIT_KEY).setValue([Layer, 0], NewValue)
        self.edited()

    def setYSplit(self, NewValue, Layer):
        self.getStandard(TEX_SPLIT_KEY).setValue([Layer, 1], NewValue)
        self.edited()

    def setTextureName(self, NewName, Layer):
        self.getStandard(TEX_NAMES_KEY).setValue([Layer], NewName)
        self.edited()


    def setXFlip(self, NewValue, AniNum, AniFrame):
        self.getStandard(ANI_FLIP_KEY).setValue([AniNum, AniFrame, 0], NewValue)
        self.edited()

    def setYFlip(self, NewValue, AniNum, AniFrame):
        self.getStandard(ANI_FLIP_KEY).setValue([AniNum, AniFrame, 1], NewValue)
        self.edited()

    def setXSize(self, NewValue, AniNum, AniFrame):
        self.getStandard(ANI_SIZE_KEY).setValue([AniNum, AniFrame, 0], NewValue)
        self.edited()

    def setYSize(self, NewValue, AniNum, AniFrame):
        self.getStandard(ANI_SIZE_KEY).setValue([AniNum, AniFrame, 1], NewValue)
        self.edited()

    def changeAniName(self, NewValue, AniNum):
        self.getStandard(ANI_NAMES_KEY).setValue([AniNum], NewValue)
        self.edited()

    def changeAniValueX(self, NewValue, AniNum, AniFrame):
        self.getStandard(ANI_VALUES_KEY).setValue([AniNum, AniFrame, 0], NewValue)
        self.edited()

    def changeAniValueY(self, NewValue, AniNum, AniFrame):
        self.getStandard(ANI_VALUES_KEY).setValue([AniNum, AniFrame, 1], NewValue)
        self.edited()

    def changeOffsetValueX(self, NewValue, AniNum, AniFrame):
        self.getStandard(ANI_OFFSETS_KEY).setValue([AniNum, AniFrame, 0], NewValue)
        self.edited()

    def changeOffsetValueY(self, NewValue, AniNum, AniFrame):
        self.getStandard(ANI_OFFSETS_KEY).setValue([AniNum, AniFrame, 1], NewValue)
        self.edited()

    def changeAniTexture(self, NewValue, AniNum, AniFrame):
        self.getStandard(ANI_TEX_NUM_KEY).setValue([AniNum, AniFrame], NewValue)
        self.edited()

    def setTime(self, NewValue, AniNum, AniFrame):
        self.getStandard(ANI_TIME_KEY).setValue([AniNum, AniFrame], NewValue)
        self.edited()

    def setCenter(self, NewValue, AniNum, AniFrame):
        self.getStandard(ANI_CENTER_KEY).setValue([AniNum, AniFrame], NewValue)
        self.edited()

    def changeAniNum(self, NewValue):
        self.getStandard(ANI_NUM_KEY).setValue(NewValue)
        self.updateAnimationNum()
        self.edited()

    def changeAniFrames(self, NewValue, AniNum):
        self.getStandard(ANI_FRAMES_KEY).setValue([AniNum], NewValue)
        self.updateFrameNum(AniNum)
        self.edited()

    def copyFrame(self, AniNum, AniFrame):
        AniValues = self.getTextureValues(AniNum, AniFrame)
        AniFlip = self.getFlip(AniNum, AniFrame)
        TexNum = self.getTextureNum(AniNum, AniFrame)
        Sizes = self.getSize(AniNum, AniFrame)
        Time = self.getTime(AniNum, AniFrame)
        Center = self.getCenter(AniNum, AniFrame)
        Offset = self.getOffset(AniNum, AniFrame)
        for i in xrange(self.getFrameNum(AniNum)):
            if i == AniFrame:
                continue
            self.changeAniValueX(AniValues[0], AniNum, i)
            self.changeAniValueY(AniValues[1], AniNum, i)
            self.changeAniTexture(TexNum, AniNum, i)
            self.setXFlip(AniFlip[0], AniNum, i)
            self.setYFlip(AniFlip[1], AniNum, i)
            self.setXSize(Sizes[0], AniNum, i)
            self.setYSize(Sizes[1], AniNum, i)
            self.changeOffsetValueX(Offset[0], AniNum, i)
            self.changeOffsetValueY(Offset[1], AniNum, i)
            self.setTime(Time, AniNum, i)
            self.setCenter(Center, AniNum, i)

    def copyAnimation(self, AniNum):
        self.changeAniFrames(self.getFrameNum(AniNum), self.copyAniNum)
        self.changeAniName(self.getAnimationName(AniNum), self.copyAniNum)
        for i in xrange(self.getFrameNum(AniNum)):
            AniValues = self.getTextureValues(AniNum, i)
            Sizes = self.getSize(AniNum, i)
            AniFlip = self.getFlip(AniNum, i)
            Offset = self.getOffset(AniNum, i)
            self.changeAniValueX(AniValues[0], self.copyAniNum, i)
            self.changeAniValueY(AniValues[1], self.copyAniNum, i)
            self.changeAniTexture(self.getTextureNum(AniNum, i), self.copyAniNum, i)
            self.setXFlip(AniFlip[0], self.copyAniNum, i)
            self.setYFlip(AniFlip[1], self.copyAniNum, i)
            self.setXSize(Sizes[0], self.copyAniNum, i)
            self.setYSize(Sizes[1], self.copyAniNum, i)
            self.changeOffsetValueX(Offset[0], self.copyAniNum, i)
            self.changeOffsetValueY(Offset[1], self.copyAniNum, i)
            self.setTime(self.getTime(AniNum, i), self.copyAniNum, i)
            self.setCenter(self.getCenter(AniNum, i), self.copyAniNum, i)

    def setCopyNum(self, AniNum):
        self.copyAniNum = AniNum

    def getCopyNum(self):
        return self.copyAniNum

    def updateAnimationNum(self):
        AniNum = self.getAnimationNum()
        self.getStandard(ANI_NAMES_KEY).setSize([], AniNum)
        self.getStandard(ANI_FRAMES_KEY).setSize([], AniNum)
        self.getStandard(ANI_VALUES_KEY).setSize([], AniNum)
        self.getStandard(ANI_OFFSETS_KEY).setSize([], AniNum)
        self.getStandard(ANI_TEX_NUM_KEY).setSize([], AniNum)
        self.getStandard(ANI_FLIP_KEY).setSize([], AniNum)
        self.getStandard(ANI_SIZE_KEY).setSize([], AniNum)
        self.getStandard(ANI_TIME_KEY).setSize([], AniNum)
        self.getStandard(ANI_CENTER_KEY).setSize([], AniNum)



    def updateFrameNum(self, AniNum):
        FrameNum = self.getFrameNum(AniNum)
        self.getStandard(ANI_VALUES_KEY).setSize([AniNum], FrameNum)
        self.getStandard(ANI_OFFSETS_KEY).setSize([AniNum], FrameNum)
        self.getStandard(ANI_TEX_NUM_KEY).setSize([AniNum], FrameNum)
        self.getStandard(ANI_FLIP_KEY).setSize([AniNum], FrameNum)
        self.getStandard(ANI_SIZE_KEY).setSize([AniNum], FrameNum)
        self.getStandard(ANI_TIME_KEY).setSize([AniNum], FrameNum)
        self.getStandard(ANI_CENTER_KEY).setSize([AniNum], FrameNum)

    def aniRemoveTexture(self, TexNum):
        for i in xrange(self.getAnimationNum()):
            for i2 in xrange(self.getFrameNum(i)):
                OldNum = self.getTextureNum(i, i2)
                if OldNum > TexNum:
                    self.changeAniTexture(OldNum - 1, i, i2)
                elif OldNum == TexNum:
                    self.changeAniTexture(0, i, i2)

    def moveImages(self):
        Pathname = self.getFilename()[:-len(SPRITE_EXTENSION)] + "_"
        Index = 0
        Files = []
        for i in range(self.getTextureLength()):
            Path = self.getTexturePath(i)
            NewPath = Pathname + str(Index) + IMAGE_EXTENSIONS
            Index = Index + 1
            if len(Path) == 0 or Path == NewPath:
                Files.append(None)
                continue
            Files.append(loadDataAsFile(Path))
            self.setTexturePath(NewPath, i)
        for i in range(len(Files)):
            if Files[i] is None:
                continue
            saveDataAsFile(Files[i], self.getTexturePath(i))

    def saveData(self):
        self.moveImages()
        super(SpriteData, self).saveData()
